package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl implements DishService {


  @Autowired
  DishMapper dishMapper;
  @Autowired
  FlavorMapper flavorMapper;
  @Autowired
  SetmealDishMapper setmealDishMapper;

  /**
   * 新增菜品和对应的口味
   * @param dto
   */
  @Override
  @Transactional
  public void saveWithFlavor(DishDTO dto) {
    Dish dish = new Dish();
    BeanUtils.copyProperties(dto, dish);
    // 向菜品表插入1条数据
    // 回显dish的id
    dishMapper.insert(dish);
    // 向口味表插入n条数据
    List<DishFlavor> flavors = dto.getFlavors();
    if(flavors != null && flavors.size() > 0){
      flavors.forEach(flavor->{
        flavor.setDishId(dish.getId());
      });
      flavorMapper.insertBatch(flavors);
    }
  }

  /**
   * 菜品分页查询
   * @param dto
   * @return
   */
  @Override
  public PageResult pageQuery(DishPageQueryDTO dto) {
    PageHelper.startPage(dto.getPage(), dto.getPageSize());
    Page<DishVO> dishVOPage = dishMapper.pageQuery(dto);
    PageResult pageResult = new PageResult(dishVOPage.getTotal(), dishVOPage.getResult());
    return pageResult;
  }

  /**
   * 菜品批量删除
   * @param ids
   */
  @Override
  @Transactional
  public void deleteBatch(List<Long> ids) {
    // 判断菜品是否在销售
    List<Dish> dishList = dishMapper.getByIds(ids);
    dishList.forEach(dish -> {
      if(Objects.equals(dish.getStatus(), StatusConstant.ENABLE)){
        throw new DeletionNotAllowedException(dish.getName() + " " + MessageConstant.DISH_ON_SALE);
      }
    });
    // 判断菜品是否关联套餐
    List<SetmealDish> setmealDishList = setmealDishMapper.getByDishIds(ids);
    if(setmealDishList != null && setmealDishList.size() > 0){
      throw new DeletionNotAllowedException(setmealDishList.get(0).getName() + " " + MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
    }
    // 删除菜品后，关联的口味数据也需要删除
    dishMapper.deleteByDishIds(ids);
    // 删除关联的口味
    flavorMapper.deleteByDishIds(ids);
  }

  /**
   * 根据id查询菜品
   * @param id
   * @return
   */
  @Override
  public DishVO getByIdWithFlavor(Long id) {
    // 根据菜品id获取dish
    List<Dish> dishList = dishMapper.getByIds(Collections.singletonList(id));
    Dish dish = null;
    if(dishList != null && dishList.size() != 0){
      dish = dishList.get(0);
    }
    // 根据菜品id获取菜品口味
    List<DishFlavor> flavors = flavorMapper.getByDishId(id);
    // 组装DishVo
    DishVO dishVO = new DishVO();
    if (dish != null) {
      BeanUtils.copyProperties(dish,dishVO);
    }
    dishVO.setFlavors(flavors);
    return dishVO;
  }

  /**
   * 更新菜品信息
   * @param dto
   */
  @Override
  public void updateWithFlavor(DishDTO dto) {
    // 更新 Dish
    Dish dish = new Dish();
    BeanUtils.copyProperties(dto,dish);
    dishMapper.update(dish);
    // 删除原有口味数据
    flavorMapper.deleteByDishIds(Collections.singletonList(dto.getId()));
    // 新增新的口味数据
    List<DishFlavor> flavors = dto.getFlavors();
    if(flavors != null && flavors.size() != 0) {
      flavors.forEach(flavor -> {
        flavor.setDishId(dto.getId());
      });
    }
    flavorMapper.insertBatch(flavors);
  }

  /**
   * 菜品起售停售
   * @param status
   * @param id
   */
  @Override
  public void startOrStop(Integer status, Long id) {
    Dish dish = Dish.builder().status(status).id(id).build();
    dishMapper.update(dish);
  }
}
